Skip to content

fix(factory): treat 404 agent_not_found on release as terminal success - #365

Merged
kjgbot merged 1 commit into
mainfrom
lane/release-404-terminal
Aug 24, 2026
Merged

fix(factory): treat 404 agent_not_found on release as terminal success#365
kjgbot merged 1 commit into
mainfrom
lane/release-404-terminal

Conversation

@kjgbot

@kjgbot kjgbot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the runaway release loop that has kept cloud Factory from dispatching any work. When FleetClient.release() returns a Relay 404 agent_not_found, that IS the successful end state release aims for — the agent is already gone from Relay's view. Treating it as a failure re-arms #abandonStuckDispatch#releaseAndTerminateAgents at ~1 Hz forever.

Evidence

An instrumented /evidence stream in AgentWorkforce/factory-cloud (fc#77) captured 1434 stderr lines in 13 minutes, 100% of them the same three ghost agents (ar-9-impl-sandbox, ar-1540-babysit-relay, ar-1540-impl-relay) cycling:

[factory] releasing agents held past deadline {issue:"9", heldForMs:373458303, ...}
[factory] no pid available to terminate ar-9-impl-sandbox during completion
[factory] failed to release ar-9-impl-sandbox during completion
  {name:"RelayError", code:"not_found", retryable:false, statusCode:404, rawCode:"agent_not_found"}

readinessReconcile.state=stalled for 30+ minutes, dispatched=0, canary #350 untouched — because every sweep cycle was consumed by this loop and discovery never got a slot.

Change

  • src/orchestrator/release-error.ts — a small isAgentAlreadyGoneOnRelease(error) classifier that recognises both the canonical rawCode === 'agent_not_found' and the fallback statusCode === 404 && code === 'not_found' (a middleware may re-throw with HTTP status preserved but drop the SDK-specific rawCode).
  • src/orchestrator/factory.ts:#releaseAndTerminateAgents — on an already-gone catch, run the same batch.recordRelease + agent.released bookkeeping the success path does, so the lifecycle marches on to abandoned and the sweep frees up. Anything else — retryable 503 host-unavailable, real 5xx, unrelated 404 on a different resource — keeps going through the failure path and drives a legitimate retry.

Test plan

  • src/orchestrator/release-error.test.ts — 7 cases: canonical shape, fallback shape, foreign 404, retryable 503, generic 500, primitives, missing fields.
  • npx vitest run src/orchestrator/release-error.test.ts — 7/7 pass.
  • After merge: publish 0.1.74, bump factory-cloud's pinned version, live Deploy Factory, verify factoryOutput.stderrLastN stops showing the release-retry pattern and readinessReconcile transitions from stalled back to healthy with real dispatched > 0.

🤖 Generated with Claude Code

A `FleetClient.release()` that comes back with a Relay 404
`agent_not_found` IS the successful end state release itself aims for:
the agent is already gone from Relay's view. Treating it as a failure
re-arms `#abandonStuckDispatch` → `#releaseAndTerminateAgents` at ~1 Hz
forever. That is what wedged cloud dispatch: an instrumented `/evidence`
stream captured 1434 stderr lines in 13 minutes, 100% of them the same
three ghost agents (`ar-9-impl-sandbox`, `ar-1540-babysit-relay`,
`ar-1540-impl-relay`) cycling through:

  [factory] releasing agents held past deadline
    {issue:"9", heldForMs:373458303, holdTimeoutMs:14400000,
     waitingForTerminalState:"human-review",
     reason:"held-past-deadline",
     agents:["ar-9-impl-sandbox"]}
  [factory] no pid available to terminate ar-9-impl-sandbox during
    completion
  [factory] failed to release ar-9-impl-sandbox during completion
    {name:"RelayError", message:'Agent "ar-9-impl-sandbox" not found',
     code:"not_found", retryable:false, statusCode:404,
     rawCode:"agent_not_found"}

Sweep discovery never got a slot because every cycle was consumed by
this loop; the canary issue and every real work unit sat untouched.

The fix classifies a release error as "already gone" when either the
canonical `rawCode === 'agent_not_found'` field is set, or the fallback
combination `statusCode === 404 && code === 'not_found'` is present (a
middleware may re-throw with the HTTP status preserved but drop the
SDK-specific rawCode; the fix depends on catching both shapes). On the
already-gone path, the code performs the exact same bookkeeping the
success path does — `batch.recordRelease` and the `agent.released`
report — so the lifecycle marches on to `abandoned` and the sweep tick
frees up. Anything else keeps going through the failure path: a
retryable 503 host-unavailable, a real 5xx, an unrelated 404 on a
different resource all still add the agent to `failed`, drive
`cleanupComplete = false`, and schedule a legitimate retry.

Isolated the classifier into its own module so the reasoning lives
next to the rule and the tests are focused (rather than adding a case
to the 20k-line factory.test.ts, which already carries known flakes).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Session-Id: a4adc660-7d8a-4fff-b674-2e3931afd6cb
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4690c72a-0ed2-445e-bc38-9ec9819973fa


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

@coderabbitai review

Requested for exact head a0a2559fc3fbc9216925b1cece1b3752da74e4cf.

@kjgbot
kjgbot merged commit c65cc5f into main Aug 24, 2026
8 checks passed
@kjgbot
kjgbot deleted the lane/release-404-terminal branch August 24, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant